package com.speaktoitcookbook.service;

import java.io.IOException;
import java.io.PrintWriter;

import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.speaktoitcookbook.bean.User;
import com.speaktoitcookbook.bean.UserInfo;
import com.speaktoitcookbook.dao.IUserDao;
import com.speaktoitcookbook.dao.factory.DaoFactory;

@WebServlet("/RegisterService")
public class RegisterService extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public RegisterService() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");

		IUserDao userDao = DaoFactory.getUserDao();

		String userId = request.getParameter("username");
		String userPwd = request.getParameter("password");

		User user = new User();
		user.setID(userId);
		user.setPassword(userPwd);
		
		UserInfo userinfo = new UserInfo();
		userinfo.setID(userId);

		boolean result ;

		try {
			result = userDao.doCreate(user, userinfo);
			if (result) {//注册用户成功

				PrintWriter out = response.getWriter();
				out.print(result);
				out.flush();
				out.close();
			} else {

			}

			System.out.println("result:" + result);
		} catch (SQLException e) {
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}